package com.lsh.ailian2.controller;

import com.lsh.ailian2.common.BaseResponse;
import com.lsh.ailian2.common.ErrorCode;
import com.lsh.ailian2.common.ResultUtils;
import com.lsh.ailian2.exception.BusinessException;
import com.lsh.ailian2.model.domain.Blog;
import com.lsh.ailian2.model.request.BlogAddRequest;
import com.lsh.ailian2.model.request.BlogEditRequest;
import com.lsh.ailian2.model.request.BlogQueryRequest;
import com.lsh.ailian2.model.request.DeleteRequest;
import com.lsh.ailian2.model.vo.BlogVO;
import com.lsh.ailian2.model.vo.LikeRequest;
import com.lsh.ailian2.model.vo.StarRequest;
import com.lsh.ailian2.model.vo.UserBlogVO;
import com.lsh.ailian2.service.BlogService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private BlogService blogService;

    /**
     * 处理添加博客的请求
     * @param blogAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addBlog(@RequestBody BlogAddRequest blogAddRequest, HttpServletRequest request) {
        if (blogAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long b = blogService.addBlog(blogAddRequest, request);
        return ResultUtils.success(b);
    }

    /**
     * 处理推荐博客的请求
     * @param blogQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/recommend")
    public BaseResponse<List<BlogVO>> listBlogs(@RequestBody BlogQueryRequest blogQueryRequest, HttpServletRequest request) {
        if (blogQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (blogQueryRequest.getPageNum() <= 0 || blogQueryRequest.getPageSize() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<BlogVO> blogVOList = blogService.listBlogs(blogQueryRequest, request);
        return ResultUtils.success(blogVOList);
    }

    /**
     * 处理获取博客详情的请求
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get/{id}")
    public BaseResponse<BlogVO> getBlogById(@PathVariable("id") Long id, HttpServletRequest request) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        BlogVO blogVO = blogService.getBlogDetailById(id, request);
        return ResultUtils.success(blogVO);
    }

    /**
     * 处理删除博客的请求
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteBlog(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = blogService.deleteBlog(deleteRequest, request);
        return ResultUtils.success(b);
    }

    /**
     * 处理收藏博客的请求
     * @param starRequest
     * @param request
     * @return
     */
    @PostMapping("/star")
    public BaseResponse<Boolean> starBlog(@RequestBody StarRequest starRequest, HttpServletRequest request) {
        if (starRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = blogService.starBlog(starRequest, request);
        return ResultUtils.success(b);
    }

    /**
     * 处理点赞博客的请求
     * @param likeRequest
     * @param request
     * @return
     */
    @PostMapping("/like")
    public BaseResponse<Boolean> likeBlog(@RequestBody LikeRequest likeRequest, HttpServletRequest request) {
        if (likeRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = blogService.likeBlog(likeRequest, request);
        return ResultUtils.success(b);
    }

    /**
     * 处理取消收藏博客的请求
     * @param starRequest
     * @param request
     * @return
     */
    @PostMapping("/star/cancel")
    public BaseResponse<Boolean> cancelStarBlog(@RequestBody StarRequest starRequest, HttpServletRequest request) {
        if (starRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = blogService.cancelStarBlog(starRequest, request);
        return ResultUtils.success(b);
    }

    /**
     * 处理取消点赞博客的请求
     *
     * @param likeRequest 包含取消点赞所需参数的请求体（需包含博客ID和用户标识），不可为空
     * @param request HTTP请求对象，用于获取请求上下文信息
     * @return 包含操作结果的响应对象：
     *         - data字段为true表示取消点赞成功
     *         - 通过ResultUtils进行标准化响应包装
     * @throws BusinessException 当likeRequest参数为空时抛出参数错误异常
     */
    @PostMapping("/like/cancel")
    public BaseResponse<Boolean> cancelLikeBlog(@RequestBody LikeRequest likeRequest, HttpServletRequest request) {
        if (likeRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = blogService.cancelLikeBlog(likeRequest, request);
        return ResultUtils.success(b);
    }

    /**
     * 获取指定用户的博客列表
     *
     * @param id 用户ID（路径参数），需为正整数
     * @param blogQueryRequest 博客查询请求体，包含分页/过滤条件等参数
     * @param request HTTP请求对象，用于获取请求上下文信息
     * @return 基础响应对象，包含博客VO列表数据
     */
    @PostMapping("/user/{id}")
    public BaseResponse<List<BlogVO>> listUserBlogs(@PathVariable("id") Long id,
                                                    @RequestBody BlogQueryRequest blogQueryRequest, HttpServletRequest request) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户不存在");
        }
        List<BlogVO> blogVOList = blogService.listUserBlogs(id, blogQueryRequest, request);
        return ResultUtils.success(blogVOList);
    }
    /**
     * 查询自己或其他用户的点赞、收藏、浏览过的博客
     * @param blogQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/interaction/list")
    public BaseResponse<List<BlogVO>> listLikedOrStarredBlogs(@RequestBody BlogQueryRequest blogQueryRequest,
                                                              HttpServletRequest request) {
        if (blogQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<BlogVO> blogVOList = blogService.listInteractionBlogs(blogQueryRequest, request);
        return ResultUtils.success(blogVOList);
    }

    /**
     * 查询用户的点赞、收藏、浏览过的博客
     */
    @PostMapping("/user/list")
    public BaseResponse<UserBlogVO> listUserInteractionBlogs(@RequestBody BlogQueryRequest blogQueryRequest,
                                                             HttpServletRequest request) {
        if (blogQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserBlogVO blogVOList = blogService.listUserInteractionBlogs(blogQueryRequest, request);
        return ResultUtils.success(blogVOList);
    }

    /**
     * 编辑博客
     * @param blogEditRequest
     * @param request
     * @return
     */
    @PostMapping("/edit")
    public BaseResponse<Long> editBlog(@RequestBody BlogEditRequest blogEditRequest, HttpServletRequest request) {
        if (blogEditRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long id = blogService.editBlog(blogEditRequest, request);
        return ResultUtils.success(id);
    }

}
